package com.qdsg.ysg.doctor.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;
import com.qdsg.ysg.doctor.ui.WebViewActivity;
import com.qdsg.ysg.doctor.util.ToastUtil;

import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


/**
 * Description
 * Created by lbj on 2021/6/30 11:13
 */

public class PermissionDialog extends BaseDialogFragment {

    public static PermissionDialog newInstance() {
        Bundle args = new Bundle();
        PermissionDialog fragment = new PermissionDialog();
        fragment.setArguments( args );
        return fragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;

    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_permission, container );
        TextView tv_confirm = view.findViewById( R.id.tv_confirm );
        TextView tv_cancel = view.findViewById( R.id.tv_cancel );
        TextView tv_permission = view.findViewById( R.id.tv_permission );
        tv_cancel.setOnClickListener( v -> {
            if (mListener != null) {
                mListener.onCancel();
                dismiss();
            }

        } );

        tv_confirm.setOnClickListener( v -> {
            if (mListener != null) {
                mListener.onConfirm();
                dismiss();
            }
        } );
        tv_permission.setOnClickListener( v -> {
            startActivity( new Intent( mContext, WebViewActivity.class ).putExtra( "fromWhere", "PermissionDialog" ).putExtra( "rulesType", "7" ) );
        } );
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );


    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        mDialog.setCanceledOnTouchOutside( false );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.CENTER );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull( Objects.requireNonNull( getDialog() ).getWindow() ).requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        Objects.requireNonNull( getDialog().getWindow() ).setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm();

        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


}
